﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;
using Newtonsoft.Json;
using static System.Net.Mime.MediaTypeNames;
using System.Drawing;
using System.Drawing.Imaging;
using System.Xml.Serialization;
using System.Reflection.Emit;

namespace EWB_library
{

    [Category("TEST")]
    public class EWB_test
    {

        public static void TEST()
        {
            MessageBox.Show("调用成功");
        }




    }


    [Category("MES")]
    public class EWB_MES
    {
        private string posturl = "http://192.168.10.51:8080/htsys/webservice/crossPoint?wsdl";



          
        public string MesReturndata { get { return MesReturndata_P; } }

        public string MesSendData { get { return MesSendData_P; } }

        private string MesReturndata_P = "";

        private string MesSendData_P = "";

        public string rMssage { get { return Message; } }

        public string rMssagetype { get { return Messagetype; } }

        private string Message = "";


        private string Messagetype = "";


        public EWB_MES(string url)
        {
            posturl = url;
        }


        public string PostData(string postData, string hadkey)
        {
            Stream stream = null;
            Stream stream2 = null;
            StreamReader streamReader = null;
            HttpWebResponse httpWebResponse = null;
            HttpWebRequest httpWebRequest = null;
            byte[] bytes = Encoding.UTF8.GetBytes(postData);
            try
            {
                httpWebRequest = (WebRequest.Create(posturl) as HttpWebRequest);
                CookieContainer cookieContainer = new CookieContainer();
                httpWebRequest.Accept = "text/xml;charset=UTF-8";
                httpWebRequest.CookieContainer = cookieContainer;
                httpWebRequest.AllowAutoRedirect = true;
                httpWebRequest.Method = "POST";
                httpWebRequest.ContentType = "text/xml;charset=UTF-8";
                httpWebRequest.Pipelined = true;
                httpWebRequest.PreAuthenticate = false;
                httpWebRequest.SendChunked = false;
                httpWebRequest.ContentLength = bytes.Length;
                httpWebRequest.Timeout = 30000;
                stream = httpWebRequest.GetRequestStream();
                stream.Write(bytes, 0, bytes.Length);
                stream.Close();
            }
            catch (Exception ex)
            {
                // System.Windows.Forms.MessageBox.Show("报错：" + ex.Message);
                Message += "MES数据发送失败：" + ex.Message + "\n";
            }
            try
            {
                httpWebResponse = (httpWebRequest.GetResponse() as HttpWebResponse);
                stream2 = httpWebResponse.GetResponseStream();
                streamReader = new StreamReader(stream2, Encoding.UTF8);
                string result = streamReader.ReadToEnd().ToString();
                string empty = string.Empty;
                return result;
            }
            catch (Exception ex2)
            {
                // System.Windows.Forms.MessageBox.Show(ex2.Message);
                string message = ex2.Message;
                Message += "MES数据接收错误：" + ex2.Message + "\n";
                return string.Empty;
            }
        }

        /// <summary>
        /// 条码验证接口
        /// </summary>
        /// <param name="equipUniqueCode">设备唯一号</param>
        /// <param name="workCenterCode">生产线</param>
        /// <param name="opCode">工序</param>
        /// <param name="workCellCode">工位</param>
        /// <param name="scanCode">条码</param>
        public void sendlabel(string equipUniqueCode, string workCenterCode, string opCode, string workCellCode, string scanCode)
        {
            this.Message = "";
            this.Messagetype = "";
            string serializedString = "";
            try
            {
                Envelope myObject = new Envelope();
                myObject.Body = new EnvelopeBody();
                myObject.Body.receiveCrossPoint = new receiveCrossPoint();
                myObject.Body.receiveCrossPoint.@params = new @params();
                myObject.Body.receiveCrossPoint.@params.equipUniqueCode = equipUniqueCode;
                myObject.Body.receiveCrossPoint.@params.workCenterCode = workCenterCode;
                myObject.Body.receiveCrossPoint.@params.opCode = opCode;
                myObject.Body.receiveCrossPoint.@params.workCellCode = workCellCode;
                myObject.Body.receiveCrossPoint.@params.scanCode = scanCode;
                myObject.Body.receiveCrossPoint.@params.param1 = ""; myObject.Body.receiveCrossPoint.@params.param2 = "";
                myObject.Body.receiveCrossPoint.@params.param3 = "";
                myObject.Body.receiveCrossPoint.@params.param4 = "";
                myObject.Body.receiveCrossPoint.@params.param5 = "";

                using (StringWriter textWriter = new StringWriter())
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(Envelope));
                    serializer.Serialize(textWriter, myObject);
                    serializedString = textWriter.ToString();
                }
            }
            catch (Exception ex)
            {
                Message += "MES发送消息生成失败！" + ex.Message;
            }
            MesSendData_P = serializedString;

            MesReturndata_P = PostData(serializedString, "");


            try
            {

                using (StringReader reader = new StringReader(MesReturndata_P))
                {
                    XmlSerializer deserializer = new XmlSerializer(typeof(S1.Envelope));
                    S1.Envelope myDeserializedObject = (S1.Envelope)deserializer.Deserialize(reader);
                    this.Message += myDeserializedObject.Body.receiveCrossPointResponse.@return.msg;
                    this.Messagetype = myDeserializedObject.Body.receiveCrossPointResponse.@return.msgType;

                }
            }
            catch (Exception ex)
            {
                Message += "MES返回消息解析失败！" + ex.Message;
            }
        }



        /// <summary>
        /// 
        /// </summary>
        /// <param name="jobId">接口ID</param>
        /// <param name="jobType">接口类型</param>
        /// <param name="probarcode">产品条码</param>
        /// <param name="result">检测结果</param>
        public void sendresult(string jobId, string jobType, string probarcode, string result ,string jpg_name)
        {
            this.Message = "";
            this.Messagetype = "";


            string serializedString = "";
            try
            {

                EWB_library.CDS.Envelope Envelope = new CDS.Envelope();


                Rootobject rootobject = new Rootobject();
                rootobject.Property1 = new Class1[1];
                rootobject.Property1[0] = new Class1();
                rootobject.Property1[0].probarcode = probarcode;
                rootobject.Property1[0].prodadte = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                rootobject.Property1[0].result = result;
                rootobject.Property1[0].pulling = "";
                rootobject.Property1[0].torque = "";
                rootobject.Property1[0].displacement = "";
                rootobject.Property1[0].depth = "";
                rootobject.Property1[0].jpg_name = jpg_name;
                string jsonString = JsonConvert.SerializeObject(rootobject.Property1);
                Envelope.Body = new CDS.EnvelopeBody();
                Envelope.Body.receiveData = new CDS.receiveData();
                Envelope.Body.receiveData.jobId = jobId;
                Envelope.Body.receiveData.jobType = jobType;
                Envelope.Body.receiveData.jobData = jsonString;
                using (StringWriter textWriter = new StringWriter())
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(EWB_library.CDS.Envelope));
                    serializer.Serialize(textWriter, Envelope);
                    serializedString = textWriter.ToString();
                }
            }
            catch (Exception ex)
            {
                Message += "MES发送消息生成失败！" + ex.Message;
            }
            MesSendData_P = serializedString;

            MesReturndata_P = PostData(serializedString, "");


            try
            {

                using (StringReader reader = new StringReader(MesReturndata_P))
                {
                    XmlSerializer deserializer = new XmlSerializer(typeof(S2.Envelope));
                    S2.Envelope myDeserializedObject = (S2.Envelope)deserializer.Deserialize(reader);
                    this.Message += myDeserializedObject.Body.receiveDataResponse.OUTPUT;

                    S2.Rootobject rootobject = JsonConvert.DeserializeObject<S2.Rootobject>(this.Message);

                    this.Messagetype = rootobject.msgType;



                }
            }
            catch (Exception ex)
            {
                Message += "MES返回消息解析失败！" + ex.Message;
            }
        }


    }





}



// 注意: 生成的代码可能至少需要 .NET Framework 4.5 或 .NET Core/Standard 2.0。
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://schemas.xmlsoap.org/soap/envelope/", IsNullable = false)]
public partial class Envelope
{

    private EnvelopeBody bodyField;

    /// <remarks/>
    public EnvelopeBody Body
    {
        get
        {
            return this.bodyField;
        }
        set
        {
            this.bodyField = value;
        }
    }
}

/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public partial class EnvelopeBody
{

    private receiveCrossPoint receiveCrossPointField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Namespace = "http://service.ws.modules.hk.io/")]
    public receiveCrossPoint receiveCrossPoint
    {
        get
        {
            return this.receiveCrossPointField;
        }
        set
        {
            this.receiveCrossPointField = value;
        }
    }
}

/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://service.ws.modules.hk.io/")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://service.ws.modules.hk.io/", IsNullable = false)]
public partial class receiveCrossPoint
{

    private @params paramsField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Namespace = "")]
    public @params @params
    {
        get
        {
            return this.paramsField;
        }
        set
        {
            this.paramsField = value;
        }
    }
}

/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class @params
{

    private string equipUniqueCodeField;

    private string workCenterCodeField;

    private string opCodeField;

    private string workCellCodeField;

    private string scanCodeField;

    private string param1Field;

    private string param2Field;

    private string param3Field;

    private string param4Field;

    private string param5Field;

    /// <remarks/>
    public string equipUniqueCode
    {
        get
        {
            return this.equipUniqueCodeField;
        }
        set
        {
            this.equipUniqueCodeField = value;
        }
    }

    /// <remarks/>
    public string workCenterCode
    {
        get
        {
            return this.workCenterCodeField;
        }
        set
        {
            this.workCenterCodeField = value;
        }
    }

    /// <remarks/>
    public string opCode
    {
        get
        {
            return this.opCodeField;
        }
        set
        {
            this.opCodeField = value;
        }
    }

    /// <remarks/>
    public string workCellCode
    {
        get
        {
            return this.workCellCodeField;
        }
        set
        {
            this.workCellCodeField = value;
        }
    }

    /// <remarks/>
    public string scanCode
    {
        get
        {
            return this.scanCodeField;
        }
        set
        {
            this.scanCodeField = value;
        }
    }

    /// <remarks/>
    public string param1
    {
        get
        {
            return this.param1Field;
        }
        set
        {
            this.param1Field = value;
        }
    }

    /// <remarks/>
    public string param2
    {
        get
        {
            return this.param2Field;
        }
        set
        {
            this.param2Field = value;
        }
    }

    /// <remarks/>
    public string param3
    {
        get
        {
            return this.param3Field;
        }
        set
        {
            this.param3Field = value;
        }
    }

    /// <remarks/>
    public string param4
    {
        get
        {
            return this.param4Field;
        }
        set
        {
            this.param4Field = value;
        }
    }

    /// <remarks/>
    public string param5
    {
        get
        {
            return this.param5Field;
        }
        set
        {
            this.param5Field = value;
        }
    }
}








public class Rootobject
{
    public Class1[] Property1 { get; set; }
}

public class Class1
{
    public string probarcode { get; set; }
    public string prodadte { get; set; }
    public string result { get; set; }
    public string pulling { get; set; }
    public string torque { get; set; }
    public string displacement { get; set; }
    public string depth { get; set; }

    public string jpg_name { get; set; }
}









